/**
 * @author
 * @createtime
 * @class ProjectWinbidForm
 * @extends Ext.Window
 * @description ProjectWinbid表单
 */
ProjectWinbidForm = Ext.extend(Ext.Window, {
	//内嵌FormPanel
	formPanel : null,
	//构造函数
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		//必须先初始化组件
		this.initUIComponents();
		ProjectWinbidForm.superclass.constructor.call(this, {
			id : 'ProjectWinbidFormWin',
			layout : 'fit',
			items : this.formPanel,
			modal : true,
			width : 600,
			height:350,
			maximizable : true,
			title : '[项目中标确认]详细信息',
			buttonAlign : 'center',
			buttons : this.buttons
		});
	},//end of the constructor
	//初始化组件
	initUIComponents : function() {
		this.formPanel = new Ext.FormPanel({
			layout : 'form',
			trackResetOnLoad : true,
			bodyStyle : 'padding:10px 10px 10px 10px',
			border : false,
			url : __ctxPath + '/project/saveProjectWinbid.do',
			id : 'ProjectWinbidForm',
			defaults : {
				anchor : '98%,98%'
			},
			defaultType : 'textfield',
			items : [{
						name : 'projectWinbid.id',
						id : 'id',
						xtype : 'hidden',
						value : this.id == null ? '' : this.id
					}, {
						fieldLabel : '工程名称',
						allowBlank : false,
						maxLength : 64,
						name : 'projectWinbid.proName',
						id : 'proName'
					}, {
						fieldLabel : '中标金额(元)',
						allowBlank : false,
						name : 'projectWinbid.winBidAmount',
						id : 'winBidAmount',
						xtype : 'numberfield',
						maxLength : 11,
						regex : /^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/
					},{
						fieldLabel : '建设单位',
						allowBlank:false,
						maxLength:64,
						name : 'projectWinbid.constructionUnit',
						id : 'constructionUnit'
					}, {
						fieldLabel : '项目立项登记申请号',
						name : 'projectWinbid.proRegisterTitle',
						id : 'proRegisterTitle',
						emptyText : '请选择项目立项登记申请号',
						allowBlank : false,
						listeners : {
							focus : function() {
								ProjectRegisterSelector.getView(function(id, proName, proRegisterTitle) {
									Ext.getCmp("proRegisterTitle").setValue(proRegisterTitle);
									Ext.getCmp("projectWinbid.projectRegister.id").setValue(id);
								}, true).show();
							}
						}
					}, {
						xtype : 'hidden',
						name : 'projectWinbid.projectRegister.id',
						id : 'projectWinbid.projectRegister.id'
					}, {
						fieldLabel : '合同工期开始',
						allowBlank:false,
						maxLength:64,
						xtype:'datefield',
						format:'Y-m-d',
						name : 'projectWinbid.contractWorkLimit',
						id : 'contractWorkLimit'
					}, {
						fieldLabel : '合同工期结束',
						allowBlank:false,
						maxLength:64,
						xtype:'datefield',
						format:'Y-m-d',
						name : 'projectWinbid.contractWorkLimitEnd',
						id : 'contractWorkLimitEnd'
					}, {
						xtype : 'container',
						autoHeight : true,
						layout : 'column',
						autoWidth : true,
						defaultType : 'label',
						style : 'padding-left:0px;padding-bottom:5px;',
						items : [{
									text : '附件:',
									width : 95,
									style : "margin-top:15px;"
								}, {
									xtype : 'hidden',
									name : 'attachIds',
									id : 'projectWinbid.attachIds'
								}, {
									xtype : 'hidden',
									name : 'attachFiles',
									id : 'projectWinbid.attachFiles'
								}, {
									xtype : 'panel',
									id : 'projectWinbid.displayAttach',
									columnWidth : .95,
									height : 65,
									frame : false,
									autoScroll : true,
									style : 'padding-left:10px;padding-top:0px;',
									html : ''
								}, {
									xtype : 'button',
									iconCls : 'btn-upload',
									text : '上传',
									handler : function() {
										var dialog = App.createUploadDialog({
											file_cat : 'flow/projectWinbid',
											callback : function(data) {
												var attachFiles = Ext.getCmp("projectWinbid.attachFiles");
												var fileIds = Ext.getCmp('projectWinbid.attachIds');
												var display = Ext.getCmp('projectWinbid.displayAttach');
												display.body.update('');
												attachFiles.setValue('');
												fileIds.setValue('');
												for (var i = 0; i < data.length; i++) {
													if (fileIds.getValue() != '') {
														fileIds.setValue(fileIds.getValue() + ',');
														attachFiles.setValue(attachFiles.getValue() + ',');
													}
													attachFiles.setValue(attachFiles.getValue() + data[i].filepath + ":" + data[i].filename.replace(/\s+/g, ""));
													fileIds.setValue(fileIds.getValue() + data[i].fileId);
													Ext.DomHelper.append(display.body, '<span><a href="#" onclick="FileAttachDetail.show(' + data[i].fileId
													+ ')">' + data[i].filename.replace(/\s+/g, "") + '</a>&nbsp;|&nbsp;</span>');
												}
											},
											permitted_max_size : 1024 * 1024 * 20
										});
										dialog.show(this);
									}
								}]
					}]
		});
		//加载表单对应的数据	
		this.initData();
		//初始化功能按钮
		this.buttons = [{
					text : '保存',
					iconCls : 'btn-save',
					handler : this.save.createCallback(this.formPanel, this)
				}, {
					text : '重置',
					iconCls : 'btn-reset',
					handler : this.reset.createCallback(this.formPanel)
				}, {
					text : '取消',
					iconCls : 'btn-cancel',
					handler : this.cancel.createCallback(this)
				}];
	},//end of the initcomponents
	/**
	 * 初始化数据
	 * 
	 * @param {}
	 *            formPanel
	 */
	initData : function() {
		//加载表单对应的数据	
		if (this.id != null && this.id != 'undefined') {
			this.formPanel.getForm().load({
				deferredRender : false,
				url : __ctxPath + '/project/getProjectWinbid.do?id=' + this.id,
				waitMsg : '正在载入数据...',
				success : function(form, action) {
					var jsonData = Ext.util.JSON.decode(action.response.responseText);
					var res = jsonData.data;
					var af = res.fileAttachs;
					var filePanel = Ext.getCmp('projectWinbid.displayAttach');
					var fileIds = Ext.getCmp("projectWinbid.attachIds");
					for (var i = 0; i < af.length; i++) {
						if (fileIds.getValue() != '') {
							fileIds.setValue(fileIds.getValue() + ',');
						}
						fileIds.setValue(fileIds.getValue() + af[i].fileId);
						Ext.DomHelper
								.append(
										filePanel.body,
										'<span><a href="#" onclick="FileAttachDetail.show('
												+ af[i].fileId
												+ ')">'
												+ af[i].fileName.replace(/\s+/g,"")+'</a>&nbsp;|&nbsp;</span>');
					}
					fileIds.originalValue = fileIds.getValue();
					if(!Ext.isEmpty(res.projectRegister)){
						Ext.getCmp('projectWinbid.projectRegister.id').setValue(res.projectRegister.id);
						Ext.getCmp('projectWinbid.projectRegister.id').originalValue=res.projectRegister.id;
					}
				},
				failure : function(form, action) {
				}
			});
		}
	},
	/**
	 * 重置
	 * 
	 * @param {}
	 *            formPanel
	 */
	reset : function(formPanel) {
		formPanel.getForm().reset();
	},
	/**
	 * 取消
	 * 
	 * @param {}
	 *            window
	 */
	cancel : function(window) {
		window.close();
	},
	/**
	 * 保存记录
	 */
	save : function(formPanel, window) {
		if (formPanel.getForm().isValid()) {
			formPanel.getForm().submit({
				method : 'POST',
				waitMsg : '正在提交数据...',
				success : function(fp, action) {
					Ext.ux.Toast.msg('操作信息', '成功保存信息！');
					var gridPanel = Ext.getCmp('ProjectWinbidGrid');
					if (gridPanel != null) {
						gridPanel.getStore().reload();
					}
					window.close();
				},
				failure : function(fp, action) {
					Ext.MessageBox.show({
						title : '操作信息',
						msg : '信息保存出错，请联系管理员！',
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.ERROR
					});
					//window.close();
				}
			});
		}
	}//end of save

});